﻿using Fonour.Application.Dtos;
using Fonour.Application.Services.Interfaces;
using Fonour.MVC.Models;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Fonour.MVC.Controllers
{
    public class MenuController : FonourControllerBase
    {
        private readonly IMenuAppService _menuAppService;

        public MenuController(IMenuAppService menuAppService)
        {
            this._menuAppService = menuAppService;
        }

        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 获取功能树JSON数据
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetMenuTreeDataAsync()
        {
            var menus = await this._menuAppService.GetAllListAsync(p => p.SerialNumber);
            List<TreeModel> treeModels = new List<TreeModel>();
            foreach (var menu in menus)
            {
                treeModels.Add(new TreeModel() { Id = menu.Id.ToString(), Text = menu.Name, Parent = menu.ParentId == Guid.Empty ? "#" : menu.ParentId.ToString() });
            }

            return Json(treeModels);
        }

        /// <summary>
        /// 获取子级功能列表
        /// </summary>
        /// <returns></returns>
        public async Task<IActionResult> GetMenusByParentAsync(Guid parentId, int startPage, int pageSize)
        {
            var result = await this._menuAppService.GetPageListAsync(startPage, pageSize, p => p.ParentId == parentId, p => p.SerialNumber);
            return Json(new
            {
                rowCount = result.TotalCount,
                pageCount = Math.Ceiling(Convert.ToDecimal(result.TotalCount) / pageSize),
                rows = result.DataList,
            });
        }

        /// <summary>
        /// 新增或编辑功能
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Edit(MenuDto dto)
        {
            if (!ModelState.IsValid)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = GetModelStateError()
                });
            }
            
            if (_menuAppService.InsertOrUpdate(dto))
            {
                return Json(new { Result = "Success" });
            }

            return Json(new { Result = "Faild" });
        }

        [HttpPost]
        public IActionResult DeleteMuti(string ids)
        {
            try
            {
                string[] idArray = ids.Split(',');
                List<Guid> delIds = new List<Guid>();
                foreach (string id in idArray)
                {
                    delIds.Add(Guid.Parse(id));
                }
                _menuAppService.DeleteBatch(delIds);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }

        [HttpPost]
        public IActionResult Delete(Guid id)
        {
            try
            {
                _menuAppService.Delete(id);
                return Json(new
                {
                    Result = "Success"
                });
            }
            catch (Exception ex)
            {
                return Json(new
                {
                    Result = "Faild",
                    Message = ex.Message
                });
            }
        }

        public async Task<ActionResult> Get(Guid id)
        {
            var dto = await _menuAppService.GetAsync(id);
            return Json(dto);
        }
    }
}
